import {
  shipping,
  find
} from '../../../api/askfor';
const util = require('../../../utils/util.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailsForm: null,
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.data.id = options.id;
    this.findClick()
    this.setData({
      id: this.data.id,
    })
  },
  trackingNoClick(data) {
    console.log(data)
    this.data.detailsForm.itemList[data.currentTarget.dataset.index].trackingNo = data.detail.value
    this.setData({
      detailsForm: this.data.detailsForm
    })
    console.log(this.data.detailsForm)
  },
  submitClick() {
    var _this = this
    var trackingNo = []
    console.log(_this.data.detailsForm)
    _this.data.detailsForm.itemList.forEach(function(item) {
      if (item.trackingNo != '' && !util.isNull(item.trackingNo)) {
        trackingNo.push(item)
      }
    })
    console.log(trackingNo)
    if (trackingNo.length != _this.data.detailsForm.itemList.length) {
      wx.showToast({
              icon: "none",
        title: '请填写物流',
      })
      return
    }
    shipping(this.data.detailsForm.itemList, this.data.id).then(res => {
      if (res.type == 'success') {
        wx.showToast({
          title: res.content,
        })
        wx.navigateBack({
          delta: 1
        })
      }
    }).catch(error => {
      wx.showToast({
              icon: "none",
        title: error.content,
      })
    })
  },
  findClick() {
    var _this = this
    find(this.data.id).then(
      res => {
        if (res.type == 'success') {
          console.log(res)
          _this.data.detailsForm = res.data.askfor
          _this.data.detailsForm.createDate = util.datetimeFormat(_this.data.detailsForm.createDate)
          _this.data.detailsForm.itemList.forEach(function(item) {
            item.thumbnail = util.thumbnail(item.thumbnail, 120, 120)
          })
          _this.setData({
            detailsForm: _this.data.detailsForm,
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },
  addressClick(data) {
    console.log(data)
    wx.setClipboardData({
      data: data.currentTarget.dataset.consignee + data.currentTarget.dataset.phone + data.currentTarget.dataset.areaname + data.currentTarget.dataset.address,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '复制成功',
            })
          }
        })
      }
    })
  }
})